package com.whjz.dcfs.jzxx.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.whjz.dcfs.jzxx.model.DcfsJzSbJbxx;
import com.whjz.dcfs.util.PageParam;

@Repository
public class DcfsJzSbJbxxDAO implements IDcfsJzSbJbxxDAO{

    @Autowired
    private SqlMapClient sqlMapClient;

    public void deleteDcfsJzSbJbxxByCond(DcfsJzSbJbxx object) throws SQLException{
        sqlMapClient.delete("deleteDcfsJzSbJbxxByCond", object);
    }

    public void deleteDcfsJzSbJbxxListByCond(List<DcfsJzSbJbxx> list) throws SQLException{
        if (null != list) {
			sqlMapClient.startBatch();
            for (int i = 0; i < list.size(); i++) {
                deleteDcfsJzSbJbxxByCond(list.get(i));
            }
			sqlMapClient.executeBatch();
        }
    }

    public List<DcfsJzSbJbxx> getDcfsJzSbJbxxListByCond(DcfsJzSbJbxx object,String fsybh,String start,String limit, String typeCode) throws SQLException{
    	//String start="0";
    	//System.out.println("-----------------------------------service---------------------");
    	String end=String.valueOf(Integer.parseInt(start)+Integer.parseInt(limit));
    	String sql="select * from (select t.*,rownum sdarn from ( select * from dcfs_jz_sb_jbxx  where 1=1 and jz_flag='0' ";
    	if(fsybh!=null&&!"".equals(fsybh)){
    		sql+="and jz_sb_bh in ("+fsybh+")";
    	}
		if(typeCode!=null&&!"".equals(typeCode)){
			sql=sql +"and yt_bh = '"+typeCode+"'";
		}
    	if(object!=null){
    	if(object.getJzsbmc()!=null&&!object.getJzsbmc().equals("")){
    		sql+=" and  jz_sb_mc like '%"+object.getJzsbmc()+"%'";
    	}
    	if(object.getJzsbqy()!=null&&!"".equals(object.getJzsbqy())){
    		sql+=" and  jz_sb_qy='"+object.getJzsbqy()+"'";
    	}
    	}
    	sql+=" order by jz_sb_qysj desc ) t where rownum <="+end+" ) where sdarn >  "+start;
    	ResultSet rs=null;
//    	System.out.println(sql);
    	List<DcfsJzSbJbxx> list=new ArrayList<DcfsJzSbJbxx>();
    	Connection conn=sqlMapClient.getDataSource().getConnection();
    	try{
    		rs=conn.prepareStatement(sql).executeQuery();
    		while(rs.next()){
    			DcfsJzSbJbxx model=new DcfsJzSbJbxx();
    			model.setBdjd(rs.getString("bd_jd"));
    			model.setBdwd(rs.getString("bd_wd"));
    			model.setJzbcgl(rs.getString("jz_bcgl"));
    			model.setJzbh(rs.getString("jz_bh"));
    			model.setJzbz(rs.getString("jz_bz"));
    			model.setJzdd(rs.getString("jz_dd"));
    			model.setJzfstxfwj(rs.getString("jz_fstx_fwj"));
    			model.setJzfstxjd(rs.getString("jz_fstx_jd"));
    			model.setJzfstxjhfs(rs.getString("jz_fstx_jhfs"));
    			model.setJzfstxjsfs(rs.getString("jz_fstx_jsfs"));
    			model.setJzfstxjsgd(rs.getString("jz_fstx_jsgd"));
    			model.setJzfstxlx(rs.getString("jz_fstx_lx"));
    			model.setJzfstxsl(rs.getString("jz_fstx_sl"));
    			model.setJzfstxwd(rs.getString("jz_fstx_wd"));
    			model.setJzfstxxpj(rs.getString("jz_fstx_xpj"));
    			model.setJzhpzt(rs.getString("jz_hp_zt"));
    			model.setJzjcdw(rs.getString("jz_jc_dw"));
    			model.setJzmc(rs.getString("jz_mc"));
    			model.setYtmc(rs.getString("yt_mc"));
    			model.setYtbh(rs.getString("yt_bh"));
    			model.setJzzppz(rs.getString("jz_zppz"));
    			model.setJzyysxx(rs.getString("jz_yys_xx"));
    			model.setJzxxpd1(rs.getString("jz_xxpd_1"));
    			model.setJzxxpd2(rs.getString("jz_xxpd_2"));
    			model.setJzsbbh(rs.getString("jz_sb_bh"));
    			model.setJzsbmc(rs.getString("jz_sb_mc"));
    			model.setJzsbazwz(rs.getString("jz_sb_azwz"));
    			model.setJzsbqy(rs.getString("jz_sb_qy"));
    			model.setJzsbqysj(rs.getString("jz_sb_qysj"));
    			model.setJzsbwllx(rs.getString("jz_sb_wllx"));
    			model.setJzsbxh(rs.getString("jz_sb_xh"));
    			model.setJzysdw(rs.getString("jz_ys_dw"));
    			model.setJztxzy(rs.getString("jz_txzy"));
    			list.add(model);
    		}
    	}catch (Exception e) {
			e.printStackTrace();
		}
        return list;
    }
    
    public int getDcfsJzSbJbxxListCount(DcfsJzSbJbxx object,String fsybh, String typeCode) throws SQLException{
    	int total=0;
    	String sql="select count(*) from dcfs_jz_sb_jbxx      where 1=1 and jz_flag='0' ";
    	if(fsybh!=null&&!fsybh.equals("")){
    		sql+="and jz_sb_bh in ("+fsybh+")";
    	}
		if(typeCode!=null&&!"".equals(typeCode)){
			sql=sql +"and yt_bh = '"+typeCode+"'";
		}
    	if(object!=null){
    	if(object.getJzsbmc()!=null&&!object.getJzsbmc().equals("")){
    		sql+=" and  jz_sb_mc like '%"+object.getJzsbmc()+"%'";
    	}
    	if(object.getJzsbqy()!=null&&!object.getJzsbqy().equals("")){
    		sql+=" and  jz_sb_qy='"+object.getJzsbqy()+"'";
    	}
    	}
    	ResultSet rs=null;
    	//System.out.println(sql);
    	Connection conn=sqlMapClient.getDataSource().getConnection();
    	try{
    		rs=conn.prepareStatement(sql).executeQuery();
    		while(rs.next()){
    			total=Integer.parseInt(rs.getString(1));
    			
    		}
    	}catch (Exception e) {
			e.printStackTrace();
		}
        return total;
    }

    public PageParam queryPageInfo(DcfsJzSbJbxx object,PageParam pageParam) throws SQLException{
		pageParam.setPageIndex(pageParam.getPageIndex());
		object.setPageIndex(pageParam.getPageIndex());
		object.setPageLast(pageParam.getPageLast());
		int totalCount = ((Integer)sqlMapClient.queryForObject("queryDcfsJzSbJbxxCount",object)).intValue();
		List<DcfsJzSbJbxx> list = sqlMapClient.queryForList("queryDcfsJzSbJbxxPage",object);
		pageParam.setResult(list);
		pageParam.setTotalCount(totalCount);
		pageParam.setTotalPage(pageParam.getTotalPage());
		return pageParam;
    }

	public void insertDcfsJzSbJbxx(DcfsJzSbJbxx object) throws SQLException {
		sqlMapClient.insert("insertDcfsJzSbJbxx", object);
	}

	public DcfsJzSbJbxx getDcfsJzSbJbxxByPK(DcfsJzSbJbxx object)
			throws SQLException {
		return (DcfsJzSbJbxx)sqlMapClient.queryForObject("getDcfsJzSbJbxxByPK", object);
	}

	public void updateDcfsJzSbJbxxByCond(DcfsJzSbJbxx object)
			throws SQLException {
		if("1".equals(object.getJzflag())){
		String[] str=object.getJzsbbh().split(",");
		for (String string : str) {
			object.setJzsbbh(string);
			object.setJzflag(object.getJzflag());
			sqlMapClient.update("updateDcfsJzSbJbxxByCond", object);
		}
		}else{
			sqlMapClient.update("updateDcfsJzSbJbxxByCond", object);
		}
	}

	public List<DcfsJzSbJbxx> getDcfsJzSbJbxxList(DcfsJzSbJbxx object)
			throws SQLException {
		return sqlMapClient.queryForList("getDcfsJzSbJbxxByCond", object);
	}

	public List<DcfsJzSbJbxx> getDcfsJzSbJbxxList_ggts(DcfsJzSbJbxx object)
			throws SQLException {
		return sqlMapClient.queryForList("getDcfsJzSbJbxxByCond_ggts", object);
	}
	
	

}
